<template>
	<div class="app-container">
		<el-form :inline="true"  class="demo-form-inline">
			<el-form-item v-if="moreSearch">
				<el-input v-model="filterText" :placeholder="$t('system.fcName')"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="addFileCategory" circle><i class="el-icon-plus" /></el-button>
			</el-form-item>
		</el-form>
		<el-tree class="filter-tree"   :data="list" :props="defaultProps" default-expand-all :filter-node-method="filterNode"
		 show-checkbox ref="tree" @check-change="nodeChange" :highlight-current="defaultProps.highlightCurrent" :check-strictly="defaultProps.checkStrictly" >
			<span class="custom-tree-node" slot-scope="{ node, data }">
				<span>
				{{ data.fcName }}</span>
				<span>
					<el-button  v-permission="'system:fileCategory:update'"  type="text"  size="mini" @click="() => editFileCategory(data)">
						{{$t('common.editBtn')}}
					</el-button>
					<el-button v-permission="'system:fileCategory:delete'"   type="text" @click="() => deleteFileCategory(data)">
						{{$t('common.delBtn')}}
					</el-button>
				</span>
			</span>
		</el-tree>

		<el-dialog width="30%" :title="$t('system.fileCategory')" :visible.sync="fileCategoryAddFormVisible" center>
			<fileCategoryEdit :fileCategoryInfo="fileCategoryInfo" />
			<span slot="footer" class="dialog-footer">
				<el-button type="danger" icon="el-icon-close" @click="fileCategoryAddFormVisible = false">{{$t('common.cancelBtn')}}</el-button>
				<el-button icon="el-icon-check" type="primary" @click="save">{{$t('common.saveBtn')}}</el-button>
			</span>
		</el-dialog>
	</div>
</template>
<script>
	import request from '@/utils/request'
	import permission from '@/directive/permission/index.js' // 权限判断指令
	import fileCategoryEdit from '@/views/system/file-category/file-category-edit.vue'
	export default {
		name: 'fileCategoryList',
		components: {
			fileCategoryEdit
		},
		directives: {
			permission
		},
		data() {
			return {
				//
				filterText: '',
				defaultProps: {
					children: 'children',
					label: 'label',
					highlightCurrent: true,
					checkStrictly: true
				},
				//更多检索条件
				moreSearch: true,
				//弹窗是否可见
				fileCategoryAddFormVisible: false,
				//表的下拉数据
				tableList: [],
				//正在编辑的用户信息
				fileCategoryInfo: {
					fcName: "",
					fcParentId: "",
				},
				//数据转译
				translation: {
					fcParentId: {},
				},
				//所有的下拉选项
				options: {
					fcParentId: [],
				},

				//数据列表
				list: null,

			}
		},
		//页面初始化加载
		created() {
			this.getList();
			this.getFcParentId();
		},
		watch: {
			filterText(val) {
				this.$refs.tree.filter(val);
			}
		},
		methods: {
			filterNode(value, data) {
				if (!value) return true;
				return data.fcName.indexOf(value) !== -1;
			},
			//查询列表
			getList() {
				request({
					url: '/system/fileCategory/queryTree',
					method: 'post',
					data: this.searchFrom
				}).then(response => {
					this.list = response.data
				})
			},
			//保存用户信息
			save() {
				var url = '/system/fileCategory/insert';
				if (this.fileCategoryInfo.fcId) {
					url = '/system/fileCategory/update';
				}
				request({
					url: url,
					method: 'post',
					data: this.fileCategoryInfo
				}).then(response => {
					const h = this.$createElement;
					this.$notify({
						title: '提示信息',
						message: h('i', {
							style: 'color: teal'
						}, response.message)
					});
					this.getList();
					this.fileCategoryAddFormVisible = false;
				})
			},
			//添加
			addFileCategory() {
				this.fileCategoryInfo = {};
				this.fileCategoryAddFormVisible = true;
			},
			//编辑
			editFileCategory( row) {
				this.fileCategoryInfo = {
					fcName: "",
					fcParentId: "",
				}
				this.fileCategoryInfo = row;
				this.fileCategoryAddFormVisible = true;
			},
			//删除
			deleteFileCategory( row) {
				this.$confirm('是否删除?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.confirmDelete(row)
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			//确认删除操作
			confirmDelete(row) {
				var url = '/system/fileCategory/delete';
				request({
					url: url,
					method: 'post',
					data: row
				}).then(response => {
					const h = this.$createElement;
					this.$notify({
						title: '提示信息',
						message: h('i', {
							style: 'color: teal'
						}, response.message)
					});
					this.getList();
					this.fileCategoryAddFormVisible = false;
				})
			},
			nodeChange() {
				this.$emit("change",this.$refs["tree"].getCheckedNodes(false))
			},
			getFcParentId() {
				request({
					url: '/system/fileCategory/queryTree',
					method: 'post'
				}).then(response => {
					this.options["fcParentId"] = response.data;
					for (var i = 0; i < this.options["fcParentId"].length; i++) {
						this.translation["fcParentId"][this.options["fcParentId"][i].fcId] = this.options["fcParentId"][i].fcName
					}
				})
			},
		}
	}
</script>
